//
//  PullUpDeviceTableView.m
//  EZ Tracker
//
//  Created by FENGYL on 2017/3/14.
//  Copyright © 2017年 FENGYL. All rights reserved.
//

#import "PullUpDeviceTableView.h"
#import "UIView+XL.h"

#define kMAX_HEIGHT [UIScreen mainScreen].bounds.size.height - 110
#define kMIN_HEIGHT 64

@interface PullUpDeviceTableView()

@property (strong, nonatomic) NSTimer *timer;
@property (assign, nonatomic) NSInteger time;

@property (strong, nonatomic) UISwipeGestureRecognizer *swipeGestureRecognizerUP;
@property (strong, nonatomic) UISwipeGestureRecognizer *swipeGestureRecognizerDOWN;
@property (strong , nonatomic) UIPanGestureRecognizer * panGestureRecognizer;
@property (assign , nonatomic) CGFloat pullUpViewBeginY;

@property (weak, nonatomic) IBOutlet UIView *pullUpView;
@property (weak, nonatomic) IBOutlet UITableView *deviceListTableView;

@end

@implementation PullUpDeviceTableView

-(NSTimer *)timer{
    
    if (!_timer) {
        
        _time = 0;
        
        _timer = [NSTimer scheduledTimerWithTimeInterval:1.0/100.0 repeats:YES block:^(NSTimer * _Nonnull timer) {
            
            _time++;
            
            if (_time > 100) {
                
                [_timer invalidate];
                
            }
            
        }];
        
    }
    
    return _timer;
    
}

- (instancetype)initWithDelegate:(id)delegate dataSource:(id)dataSource
{
    self = [super init];
    if (self) {
        
        self = [[[NSBundle mainBundle]loadNibNamed:@"PullUpDeviceTableView" owner:nil options:nil] lastObject];
        
        self.frame = CGRectMake(0, kMAX_HEIGHT, [UIScreen mainScreen].bounds.size.width, self.pullUpView.height);
        
        self.panGestureRecognizer = [[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(setUpGestureRecognizer:)];
        
        
        
        self.swipeGestureRecognizerUP = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(setUpSwipeGestureRecognizer:)];
        self.swipeGestureRecognizerUP.direction = UISwipeGestureRecognizerDirectionUp;
        
        
        self.swipeGestureRecognizerDOWN = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(setUpSwipeGestureRecognizer:)];
        self.swipeGestureRecognizerDOWN.direction = UISwipeGestureRecognizerDirectionDown;
        
        
        //手势优先级
        [self.panGestureRecognizer requireGestureRecognizerToFail:self.swipeGestureRecognizerUP];
        [self.panGestureRecognizer requireGestureRecognizerToFail:self.swipeGestureRecognizerDOWN];
        
        
        
        [self.pullUpView addGestureRecognizer:_panGestureRecognizer];
        
//        [self.pullUpView addGestureRecognizer:_swipeGestureRecognizerUP];
        
//        [self.pullUpView addGestureRecognizer:_swipeGestureRecognizerDOWN];
        
        self.pullUpViewBeginY = self.y;
        
        _deviceListTableView.tableFooterView = [[UIView alloc]init];
        _deviceListTableView.delegate = delegate;
        _deviceListTableView.dataSource = dataSource;
        
    }
    return self;
}

#pragma mark 刷新列表
-(void)reloadData{
    
    [self.deviceListTableView reloadData];
    
}

//-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
//    
//    self.pullUpView.alpha = 1;
//    
//}

#pragma mark 手势
-(void)setUpGestureRecognizer:(UIPanGestureRecognizer *)gesture{
    
//    [self.timer fire];
    
    
    
    CGPoint translation = [gesture translationInView:gesture.view];
    NSLog(@"%@",[NSString stringWithFormat:@"(%0.0f, %0.0f)", translation.x, translation.y]);
    
    self.y = self.pullUpViewBeginY + translation.y;
    
    if (self.y < kMIN_HEIGHT) {
        
//        self.y =[[UIApplication sharedApplication] statusBarFrame].size.height;
        
        self.y = kMIN_HEIGHT;
        
    }else if (self.y > kMAX_HEIGHT){
        
        self.y = kMAX_HEIGHT;
        
    }
    
    
    if (gesture.state == UIGestureRecognizerStateBegan) {
        
        [self.timer fire];
        
    }
    
    
    if (gesture.state == UIGestureRecognizerStateEnded) {
        
        [self.timer invalidate];
        
        self.timer = nil;
        
        NSLog(@"time %ld",(long)self.time);
        
        if (self.time > 30) {
            
            //拖
            if (self.pullUpViewBeginY + translation.y > [UIScreen mainScreen].bounds.size.height / 2) {
                
                [UIView animateWithDuration:0.3 animations:^{
                    
                    self.y = kMAX_HEIGHT;
                    
                } completion:^(BOOL finished) {
                    
                    self.pullUpViewBeginY = self.y;
                    
                }];
                
            }else{
                
                [UIView animateWithDuration:0.3 animations:^{
                    
                    self.y = kMIN_HEIGHT;
                    
                } completion:^(BOOL finished) {
                    
                    self.pullUpViewBeginY = self.y;
                    
                }];
                
                
                
                
            }
            
        }else{
            
            //扫
            if (translation.y > 0) {
                
                [UIView animateWithDuration:0.3 animations:^{
                    
                    self.y = kMAX_HEIGHT;
                    
                } completion:^(BOOL finished) {
                    
                    self.pullUpViewBeginY = self.y;
                    
                }];
                
            }else if (translation.y < 0) {
                
                [UIView animateWithDuration:0.3 animations:^{
                    
                    self.y = kMIN_HEIGHT;
                    
                } completion:^(BOOL finished) {
                    
                    self.pullUpViewBeginY = self.y;
                    
                }];
                
            }
            
            
        }
    
        
        
        
    }
    
    if (self.y == kMAX_HEIGHT) {
        
        self.pullUpView.alpha = 0.4;
        
        self.height = self.pullUpView.height;
        
    }else{
        
        self.pullUpView.alpha = 1;
        
        self.height = [UIScreen mainScreen].bounds.size.height - kMIN_HEIGHT;
        
    }
    
    self.pullUpDeviceTableViewChangeCoverViewAlpha(self.y);
    
    CGFloat totalValue = [UIScreen mainScreen].bounds.size.height - kMIN_HEIGHT - 110;
    
    self.deviceListTableView.alpha = (totalValue - self.y + kMIN_HEIGHT) / totalValue;
    
}

#pragma mark 手势扫
-(void)setUpSwipeGestureRecognizer:(UISwipeGestureRecognizer *)gesture{
    
    
    if (gesture == self.swipeGestureRecognizerUP) {
        
        
        [UIView animateWithDuration:0.3 animations:^{
            
            self.y = [UIScreen mainScreen].bounds.size.height - 110;
            
        } completion:^(BOOL finished) {
            
            self.pullUpViewBeginY = self.y;
            
        }];
        
        
    }else if (gesture == self.swipeGestureRecognizerDOWN){
        
        [UIView animateWithDuration:0.3 animations:^{
            
            self.y = [[UIApplication sharedApplication] statusBarFrame].size.height;
            
        } completion:^(BOOL finished) {
            
            self.pullUpViewBeginY = self.y;
            
        }];
        
    }
    
    if (self.y == [UIScreen mainScreen].bounds.size.height - 110) {
        
        self.pullUpView.alpha = 0.4;
        
        self.height = self.pullUpView.height;
        
    }else{
        
        self.pullUpView.alpha = 1;
        
        self.height = [UIScreen mainScreen].bounds.size.height - [UIApplication sharedApplication].statusBarFrame.size.height;
        
    }
    
    self.pullUpDeviceTableViewChangeCoverViewAlpha(self.y);
    
    CGFloat totalValue = [UIScreen mainScreen].bounds.size.height - [UIApplication sharedApplication].statusBarFrame.size.height - 110;
    
    self.deviceListTableView.alpha = (totalValue - self.y) / totalValue;
    
}



@end
